package com.huizu.project.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.common.utils.DateUtils;
import com.huizu.common.utils.SecurityUtils;
import com.huizu.common.utils.SnowFlake;
import com.huizu.project.domain.ProUser;
import com.huizu.project.domain.ProUserTer;
import com.huizu.project.domain.ProXxtz;
import com.huizu.project.mapper.ProUserMapper;
import com.huizu.project.mapper.ProUserTerMapper;
import com.huizu.project.mapper.ProXxtzMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huizu.project.mapper.ProNoticeMapper;
import com.huizu.project.domain.ProNotice;
import com.huizu.project.service.IProNoticeService;

import static com.huizu.common.utils.PageUtils.startPage;

/**
 * 公告管理Service业务层处理
 * 
 * @author zhangmx
 * @date 2024-05-25
 */
@Service
public class ProNoticeServiceImpl implements IProNoticeService 
{
    @Autowired
    private ProNoticeMapper proNoticeMapper;

    @Autowired
    private ProUserMapper proUserMapper;

    @Autowired
    private ProXxtzMapper proXxtzMapper;

    @Autowired
    private ProUserTerMapper proUserTerMapper;
    /**
     * 查询公告管理
     * 
     * @param id 公告管理主键
     * @return 公告管理
     */
    @Override
    public ProNotice selectProNoticeById(String id)
    {
        return proNoticeMapper.selectProNoticeById(id);
    }

    /**
     * 查询公告管理列表
     * 
     * @param proNotice 公告管理
     * @return 公告管理
     */
    @Override
    public List<ProNotice> selectProNoticeList(ProNotice proNotice)
    {
        return proNoticeMapper.selectProNoticeList(proNotice);
    }

    /**
     * 新增公告管理
     * 
     * @param proNotice 公告管理
     * @return 结果
     */
    @Override
    public int insertProNotice(ProNotice proNotice)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        proNotice.setCreateBy(user.getUserId().toString());
        proNotice.setId(String.valueOf(SnowFlake.nextId()));
        proNotice.setCreateTime(DateUtils.getNowDate());
        return proNoticeMapper.insertProNotice(proNotice);
    }

    /**
     * 修改公告管理
     * 
     * @param proNotice 公告管理
     * @return 结果
     */
    @Override
    public int updateProNotice(ProNotice proNotice)
    {
        return proNoticeMapper.updateProNotice(proNotice);
    }

    /**
     * 批量删除公告管理
     * 
     * @param ids 需要删除的公告管理主键
     * @return 结果
     */
    @Override
    public int deleteProNoticeByIds(String[] ids)
    {
        return proNoticeMapper.deleteProNoticeByIds(ids);
    }

    /**
     * 删除公告管理信息
     * 
     * @param id 公告管理主键
     * @return 结果
     */
    @Override
    public int deleteProNoticeById(String id)
    {
        return proNoticeMapper.deleteProNoticeById(id);
    }

    /**
     * 查询公告管理列表
     *
     * @param proNotice 公告管理
     * @return 公告管理
     */
    @Override
    public List<ProNotice> selectProNoticeListApi(ProNotice proNotice)
    {
        List<ProXxtz> proXxtzList = new ArrayList<>();

        ProUser proUser = proUserMapper.selectProUserByUserId(proNotice.getBeUserId());
        startPage();
        List<ProNotice> proNotices = proNoticeMapper.selectProNoticeList(proNotice);
        if (proUser!=null){
            ProXxtz proXxtzPram = new ProXxtz();
            proXxtzPram.setUserId(proUser.getUserId());
            List<String> collect = proXxtzMapper.selectProXxtzList(proXxtzPram).stream().map(ProXxtz::getpId).collect(Collectors.toList());
            for (ProNotice notice : proNotices) {
                if (!collect.contains(notice.getId())){
                    ProXxtz proXxtz = new ProXxtz();
                    proXxtz.setId(String.valueOf(SnowFlake.nextId()));
                    proXxtz.setUserId(proUser.getUserId());
                    proXxtz.setpId(notice.getId());
                    proXxtz.setCreateTime(new Date());
                    proXxtzList.add(proXxtz);
                }
            }
        }else {
            ProUserTer proUserTer = proUserTerMapper.selectProUserTerByUserId(proNotice.getBeUserId());
            if (proUserTer!=null){
                ProXxtz proXxtzPram = new ProXxtz();
                proXxtzPram.setUserId(proUserTer.getUserId());
                List<String> collect = proXxtzMapper.selectProXxtzList(proXxtzPram).stream().map(ProXxtz::getpId).collect(Collectors.toList());
                for (ProNotice notice : proNotices) {
                    if (!collect.contains(notice.getId())){
                        ProXxtz proXxtz = new ProXxtz();
                        proXxtz.setId(String.valueOf(SnowFlake.nextId()));
                        proXxtz.setUserId(proUserTer.getUserId());
                        proXxtz.setpId(notice.getId());
                        proXxtz.setCreateTime(new Date());
                        proXxtzList.add(proXxtz);
                    }
                }
            }
        }
        new Thread(()->{
            if (!proXxtzList.isEmpty()){
                proXxtzMapper.batchProXxtz(proXxtzList);
            }
        }).start();

        return proNotices;
    }
}
